package com.example.tao_city.spinnertext;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;

/**
 * Created by TAO_CITY on 2018/2/6.
 * progressBar不能拖拉只能系统控制进度
 */

public class ProgressBarActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    private ProgressBar pb;
    private Button add10,reset;
    private int a=0;
    private SeekBar seekBar;
    private TextView text1,text2,time,timenow;
    private MediaPlayer mp;
    private boolean run=false;
    private Handler handler;
    private int b=0;
    private Button begin,stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar);
        initButton();

    }

    @Override
    protected void onPause() {
        super.onPause();
        run=false;
    }

    private void initButton() {
        pb=findViewById(R.id.progressbar);
        pb.setMax(100);
        handler=new Handler();
        add10=findViewById(R.id.pgb_add10);
        reset=findViewById(R.id.pgb_reset);
        add10.setOnClickListener(this);
        reset.setOnClickListener(this);
        seekBar=findViewById(R.id.seekbar);
        text1=findViewById(R.id.text1);
        text2=findViewById(R.id.text2);
        time=findViewById(R.id.time);
        timenow=findViewById(R.id.timenow);
        seekBar.setOnSeekBarChangeListener(this);
        mp=MediaPlayer.create(this,R.raw.huataixiang);
        seekBar.setMax(mp.getDuration());
        time.setText("总时间："+getFormatMMSS(Long.valueOf(mp.getDuration())));
        seekBar.setProgress(0);
        begin=findViewById(R.id.begin);
        stop=findViewById(R.id.stop);
        begin.setOnClickListener(this);
        stop.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pgb_add10:
                if(a<pb.getMax()){
                    a+=10;
                    pb.setProgress(a);
                }else {
                    Toast.makeText(this,"已经满了",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.pgb_reset:
                pb.setProgress(0);
                a=0;
                break;
            case R.id.begin:
                mp.start();
                run=true;
                ra.run();
                break;
            case R.id.stop:
                run=false;
                mp.pause();
                break;
        }
    }

    @Override//进度改变
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        text1.setText("进度改变");
        text2.setText("当前进度："+getFormatMMSS(Long.valueOf(progress)));
        timenow.setText(getFormatMMSS(Long.valueOf(progress)));

    }

    @Override//开始滑动
    public void onStartTrackingTouch(SeekBar seekBar) {
        text1.setText("开始滑动");
    }

    @Override//停止滑动
    public void onStopTrackingTouch(SeekBar seekBar) {
        text1.setText("停止滑动");
        mp.seekTo(seekBar.getProgress());
    }
    //把音乐的时间格式化
    public String getFormatMMSS(Long needFromatTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        String formatTime = formatter.format(needFromatTime);
        return formatTime;
    }
    Runnable ra=new Runnable() {
        @Override
        public void run() {
            if(run && mp.getCurrentPosition()<=mp.getDuration()){
                seekBar.setProgress(mp.getCurrentPosition());
                handler.postDelayed(this,1000);
            }
        }
    };
}
